package n6_monitor对象头_偏向锁;


import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

import static utils.Sleeper.sleep;

/**
 * 偏向锁
 * JAVA代码启动后 两三秒才会启动 偏向锁
 * -
 * 偏向锁是默认是延迟的，不会在程序启动时立即生效，如果想避免延迟，可以加 VM 参数  -XX:BiasedLockingStartupDelay=0
 * 关闭指针压缩，将会 Klass Pointer占用8字节 ；关闭指针压缩的 VIM参数：-XX:-UseCompressedOops
 *
 18:18:08.517 [main] c.T1 - n6_monitor对象头_偏向锁.Dog object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
 0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
 4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
 8     4        (object header)                           a4 8b 01 f8 (10100100 10001011 00000001 11111000) (-134116444)
 12     4        (loss due to the next object alignment)
 Instance size: 16 bytes
 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total


######   VM options: -XX:BiasedLockingStartupDelay=0   之后 打印如下 -------------------------------------------------------
 18:41:34.909 [main] c.T1 - n6_monitor对象头_偏向锁.Dog object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
 0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
 4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
 8     4        (object header)                           a4 8b 01 f8 (10100100 10001011 00000001 11111000) (-134116444)
 12     4        (loss due to the next object alignment)
 Instance size: 16 bytes
 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total


 */
@Slf4j(topic = "c.T1")
public class T1_偏向锁_biased {

    // 打印 Mark Word 头
    public static void main(String[] args) throws InterruptedException {

        Dog d = new Dog();
        //d.hashCode();   // 会禁用掉 【偏向锁】  （当一个可偏向的对象，调用了hashCode()方法后 就会撤销偏向状态）


        log.debug(ClassLayout.parseInstance(d).toPrintable());

        synchronized (d){

            log.debug(ClassLayout.parseInstance(d).toPrintable());
        }

        log.debug(ClassLayout.parseInstance(d).toPrintable());

    }
}


class Dog {

}



